#include "threadPool.h"
#include<func.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>

int is_empty_directory(const char *dirname) {
    int n = 0;
    struct dirent *d;
    DIR *dir = opendir(dirname);
    if (dir == NULL) // 如果打开目录失败
        return -1;
    while ((d = readdir(dir)) != NULL) { // 遍历目录
        if(++n > 2) // 如果有大于2个文件/文件夹，说明不是空目录
            break;
    }
    closedir(dir);
    if (n <= 2) // 目录为空
        return 1;
    else
        return 0;
}

int remove_my(char *path,int netfd) {
    
    struct stat st;
    if(stat(path, &st) == -1) {
        printf("Error: File not found.\n");
        train_t train;
        bzero(&train, sizeof(train));
        train.length = strlen("Error: File not found.");
        strcpy(train.data, "Error: File not found.");
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
        return 1;
    }
    if(S_ISREG(st.st_mode)) { // 如果是文件
        if(remove(path) == 0)
        {
            printf("File deleted successfully.\n");
            train_t train;
            bzero(&train, sizeof(train));
            train.length = strlen("File deleted successfully.");
            strcpy(train.data, "File deleted successfully.");
            send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
        } // 直接删除
        else
        {
            printf("Error: Unable to delete file.\n");
            train_t train;
            bzero(&train, sizeof(train));
            train.length = strlen("Error: Unable to delete file.");
            strcpy(train.data, "Error: Unable to delete file.");
            send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);

        }
            
    }
    else if(S_ISDIR(st.st_mode)) { // 如果是目录
        if(is_empty_directory(path) == 1) { // 如果是空目录
            if(rmdir(path) == 0)
            {
                printf("Directory deleted successfully.\n");
                train_t train;
                bzero(&train, sizeof(train));
                train.length = strlen("Directory deleted successfully.");
                strcpy(train.data, "Directory deleted successfully.");
                send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
            } // 删除目录
                
            else
            {
                printf("Error: Unable to delete directory.\n");
                train_t train;
                bzero(&train, sizeof(train));
                train.length = strlen("Error: Unable to delete directory.");
                strcpy(train.data, "Error: Unable to delete directory.");
                send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
            }
                
        }
        else
        {
            printf("Error: Directory is not empty.\n");
            train_t train;
            bzero(&train, sizeof(train));
            train.length = strlen("Error: Directory is not empty.");
            strcpy(train.data, "Error: Directory is not empty.");
            send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
        }
            
    }
    else {
        printf("Error: Unsupported file type.\n");
        train_t train;
        bzero(&train, sizeof(train));
        train.length = strlen("Error: Unsupported file type.");
        strcpy(train.data, "Error: Unsupported file type.");
        send(netfd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
        return 1;
    }
    return 0;
}